%% Author: vivimice
%% Created: Jan 10, 2012
%% Description: TODO: Add description to context
-module(context).

-record(context_values, {socket, receiver, sender, window_rows, window_columns}).

-behaviour(gen_server).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([start_link/0, init/1, handle_call/3, terminate/2, handle_cast/2, handle_info/2, code_change/3]).
-export([get_socket/1, set_socket/2]).
-export([get_sender/1, set_sender/2]).
-export([get_receiver/1, set_receiver/2]).
-export([get_window_size/1, set_window_size/2]).
-export([stop/1]).

%%
%% API Functions
%%
-spec start_link() -> Pid::pid().
start_link() ->
	{ok, Pid} = gen_server:start_link(?MODULE, [], []),
	Pid.

init(_Args) ->
	ContextValues = #context_values{},
	{ok, ContextValues}.

get_socket(Context) ->
	{socket, Socket} = gen_server:call(Context, get_socket),
	Socket.

set_socket(Context, Socket) ->
	ok = gen_server:call(Context, {set_socket, Socket}).

get_sender(Context) ->
	{sender, Sender} = gen_server:call(Context, get_sender),
	Sender.

set_sender(Context, Sender) ->
	ok = gen_server:call(Context, {set_sender, Sender}).

get_receiver(Context) ->
	{receiver, Receiver} = gen_server:call(Context, get_receiver),
	Receiver.

set_receiver(Context, Receiver) ->
	ok = gen_server:call(Context, {set_receiver, Receiver}).

get_window_size(Context) ->
	{window_size, {Rows, Columns}} = gen_server:call(Context, get_window_size),
	{Rows, Columns}.

%% set_window_size(Context, {Rows, Columns})
set_window_size(Context, {Rows, Columns}) ->
	ok = gen_server:call(Context, {set_window_size, {Rows, Columns}}).

%%%%%%%%%%%%%%%%%%%%%%%%%

handle_call(get_socket, _From, ContextValues) ->
	{reply, {socket, ContextValues#context_values.socket}, ContextValues};

handle_call({set_socket, Socket}, _From, ContextValues) ->
	{reply, ok, ContextValues#context_values{socket = Socket}};

handle_call(get_sender, _From, ContextValues) ->
	{reply, {sender, ContextValues#context_values.sender}, ContextValues};

handle_call({set_sender, Sender}, _From, ContextValues) ->
	{reply, ok, ContextValues#context_values{sender = Sender}};

handle_call(get_receiver, _From, ContextValues) ->
	{reply, {receiver, ContextValues#context_values.receiver}, ContextValues};

handle_call({set_receiver, Receiver}, _From, ContextValues) ->
	{reply, ok, ContextValues#context_values{receiver = Receiver}};

handle_call(get_window_size, _From, ContextValues) ->
	{reply, {window_size, {ContextValues#context_values.window_rows, ContextValues#context_values.window_columns}}, ContextValues};

handle_call({set_window_size, {Rows, Columns}}, _From, ContextValues) ->
	{reply, ok, ContextValues#context_values{window_rows = Rows, window_columns = Columns}};

handle_call(_Other, _From, ContextValues) ->
	{reply, undef, ContextValues}.

%%%%%%%%%%%%%%%%%%%%%%%%%%

stop(Context) ->
	gen_server:cast(Context, stop).

handle_cast(stop, ContextValues) ->
	{stop, normal, ContextValues}.

handle_info(_Info, State) ->
	{noreply, State}.

code_change(_OldVsn, State, _Extra) ->
	{ok, State}.
terminate(normal, _ContextValues) ->
	ok.
